//
// Created by xbm on 23-3-9.
//

#include <memory>
#include <future>
#include <thread>
#include <iostream>
#include <vector>
#include <mutex>
#include <atomic>
#include <unordered_map>
#include "timeit.h"
#include "threadPool.h"

using namespace std;
using namespace util;

void test(int times) {
    auto &pool = threadPool::ThreadPool::getInstance();
    pool.init(4);
    auto do_test = [&]() {
        unordered_map<thread::id,atomic_int>um;
        mutex mx;
        auto task = [&]() {
            cout << this_thread::get_id() << endl;
            //lock_guard lg(mx);
            ++um[this_thread::get_id()];
        };
        vector<future<void>>vec;
        for (int i = 0; i < times; ++i) {
            auto job = make_shared<packaged_task<void()>>(task);
            vec.push_back(pool.commit(job));
        }
        for(auto &fut:vec){
            fut.get();
        }
        int all=0;
        for(auto&[id,time]:um) {
            cout<<"id "<<id<<" has run "<<time<<" times"<<endl;
            all+=time;
        }
        cout << "all run times " << all << endl;
    };

    cout<<"threadPool test begin"<<endl;
    time_it(do_test);
    cout<<"threadPool test end"<<endl;

}

int main() {
    test(100000);
    //test(1000000);
    //test(10000000);
}
